package com.adms;

import java.io.File;
import java.util.EnumSet;
import java.util.List;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.adms.controller.BasicErrorController;
import com.adms.redis.RedisCache;
import com.adms.shiro.RememberMeFilter;
import com.adms.util.FileUtil;
import com.adms.websocket.WebSocketServer;

@EnableScheduling
@EnableWebSocket
@SpringBootApplication
public class AdmsApplication implements ServletContextInitializer, WebMvcConfigurer, WebSocketConfigurer {
	
	@Autowired
    private Environment env;
	@Autowired
	private RedisTemplate<Object,Object> redisTemplate;

	public static void main(String[] args) {
		SpringApplication.run(AdmsApplication.class, args);
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowCredentials(true);
		WebMvcConfigurer.super.addCorsMappings(registry);
	}

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(webSocketServer(), "/webSocketServer").setAllowedOrigins("*");
	}

	@Bean
	public WebSocketServer webSocketServer() {
		return new WebSocketServer();
	}

	@Bean
	public RememberMeFilter rememberMeFilter() {
		return new RememberMeFilter();
	}

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		servletContext.addFilter("rememberMeFilter", rememberMeFilter())
				.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
		//启动后初始化FileUtil
		FileUtil.RES_SERVER = env.getProperty("res_server");
		FileUtil.IMG_PATH = env.getProperty("img_path");
		FileUtil.PDF_PATH = env.getProperty("pdf_path");
		FileUtil.VIDEO_PATH = env.getProperty("video_path");
		File f = new File(FileUtil.IMG_PATH);
		f.mkdirs();
		f = new File(FileUtil.PDF_PATH);
		f.mkdirs();
		f = new File(FileUtil.VIDEO_PATH);
		f.mkdirs();
		
		//初始化redisCache
		RedisCache.setRedisTemplate(redisTemplate);
	}

	@Autowired(required = false)
	private List<ErrorViewResolver> errorViewResolvers;
	private final ServerProperties serverProperties;

	public AdmsApplication(ServerProperties serverProperties) {
		this.serverProperties = serverProperties;
	}

	@Bean
	public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
		return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);
	}

	@Bean
	public TaskScheduler taskScheduler() {
		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
		taskScheduler.setPoolSize(10);
		taskScheduler.initialize();
		return taskScheduler;
	}

}
